package lanou.giftsay_01.adapter;

import android.content.Context;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.ImageView;
import android.widget.TextView;

import com.squareup.picasso.Picasso;

import java.util.ArrayList;

import cn.bmob.v3.BmobUser;
import de.hdodenhof.circleimageview.CircleImageView;
import lanou.giftsay_01.bean.GuideBean;
import lanou.giftsay_01.R;
import lanou.giftsay_01.fragment.body.OnClickGuideItem;
import lanou.giftsay_01.tools.GiftUser;
import lanou.giftsay_01.tools.GongLue;

/**
 * Created by dllo on 16/8/16.
 * 复用的适配器
 */
public class MyLvadapter  extends BaseAdapter{
    ArrayList<GuideBean> arrayList ;
    ArrayList<Boolean> checked;
    Context context ;
    private TextView textView;
    private ImageView imageView;
    private TextView view;
    private TextView titleTv;
    private TextView categoryTv;
    private TextView likeCountTv;
    private OnClickGuideItem onClickGuideItem ;
    private ImageView heart;
    private CheckBox checkBox;
    private boolean b ;
    private GongLue gongLue;

    public void setChecked(ArrayList<Boolean> checked) {
        this.checked = checked;
    }

    public void setOnClickGuideItem(OnClickGuideItem onClickGuideItem) {
        this.onClickGuideItem = onClickGuideItem;
    }

    public MyLvadapter(Context context) {
        this.context = context;
    }

    public void setArrayList(ArrayList<GuideBean> arrayList) {
        this.arrayList = arrayList;
        notifyDataSetChanged();
    }

    @Override
    public int getCount() {
        return arrayList!= null ? arrayList.get(0).getData().getItems().size() : 0;

    }

    @Override
    public Object getItem(int position) {
        return null;
    }

    @Override
    public long getItemId(int position) {
        return 0;
    }

    @Override
    public View getView(final int position, View convertView, ViewGroup parent) {
        if (convertView == null){
            convertView = LayoutInflater.from(context).inflate(R.layout.guidelistview_item ,parent ,false);
        }
        try {
//            checkBox = (CheckBox) convertView.findViewById(R.id.select_heart_item);
//            checkBox.setChecked(checked.get(position));
//            checkBox.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
//                @Override
//                public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
//                    if (isChecked){
//                        checkBox.setBackgroundResource(R.mipmap.ic_action_compact_favourite_selected);
//                    }
//                }
//            });
//            checkBox.setOnClickListener(new View.OnClickListener() {
//                @Override
//                public void onClick(View v) {
//                    CheckBox mcheckBox =  (CheckBox) v;
//               if (b){
//                     checked.set(position ,false );
//                    b= false ;
//               }else {
////                   GiftUser giftUser = BmobUser.getCurrentUser(GiftUser.class);
////                   if (giftUser != null){
////                       gongLue = new GongLue();
////                       gongLue.setName(arrayList.get(0).getData().getItems().get(position).getAuthor().getNickname());
////                       gongLue.setNickname(giftUser.getUsername());
////                       gongLue.setTime(System.currentTimeMillis());
////                       gongLue.setTitle(arrayList.get(0).getData().getItems().get(position).getTitle());
////                       gongLue.setUrl(arrayList.get(0).getData().getItems().get(position).getCover_image_url());
////                   }
//                   checked.set(position ,mcheckBox.isChecked());
//                    b = true ;
//               }
//
//                }
//            });

            textView = (TextView) convertView.findViewById(R.id.select_item_tv);
        textView.setText(arrayList.get(0).getData().getItems().get(position).getTitle());
            imageView = (ImageView) convertView.findViewById(R.id.lv_iv);
        Picasso.with(context).load(arrayList.get(0).getData().getItems().get(position).getCover_image_url()).into(imageView);
            view = (TextView) convertView.findViewById(R.id.nickname_tv);
        view.setText(arrayList.get(0).getData().getItems().get(position).getAuthor().getNickname());
            titleTv = (TextView) convertView.findViewById(R.id.title_tv);
        titleTv.setText(arrayList.get(0).getData().getItems().get(position).getColumn().getTitle());
         CircleImageView mv = (CircleImageView) convertView.findViewById(R.id.avatar_iv);
        Picasso.with(context).load(arrayList.get(0).getData().getItems().get(position).getAuthor().getAvatar_url()).into(mv);
            categoryTv = (TextView) convertView.findViewById(R.id.tv_category);
        categoryTv.setText(arrayList.get(0).getData().getItems().get(position).getColumn().getCategory());
            likeCountTv = (TextView) convertView.findViewById(R.id.like_count_tv);
        likeCountTv.setText(String.valueOf(arrayList.get(0).getData().getItems().get(position).getLikes_count())
                );   } catch (NullPointerException e) {
            Log.d("MyLvadapter", "没有数据" + position);
        }
        imageView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                onClickGuideItem.onClick(position , arrayList);
            }
        });

        return convertView;
    }


}
